'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 选择BOSS战出战士兵
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../consts/code');
var atkData = require('../../../libs/war/atkData');
var bossData = require('../../../libs/war/bossData');
var constantUtil = require('../../../util/constantUtil');
var bossRecordModel = require('../../../models/bossRecordModel');
var PlayerManager = require('../../../libs/playerManager');
var bossService = require('../../../services/bossService');
var soldierService = require('../../../services/soldierService');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 选择BOSS战出战士兵
 */
Handler.prototype.selectBossSoldier = function (msg, session, next) {
    var role_id = session.uid;
    var select_soldier_list = msg.select_soldier_list;

    if (!select_soldier_list || select_soldier_list.length === 0) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var boss_soldier_power;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['boss', 'soldier', 'accrued'], cb);
            },
            function (cb) {
                var boss_model = player_manager.boss;
                var soldier_model = player_manager.soldier;

                var soldier_type_error = false;
                _.forEach(select_soldier_list, function (soldier_type) {
                    if (!soldier_model.getSoldier(soldier_type)) {
                        soldier_type_error = true;
                        return false;
                    }
                    return true;
                });

                if (!!soldier_type_error) {
                    return next(null, {
                        code: Code.SOLDIER_NOT_EXIST
                    });
                }

                var soldier_count = select_soldier_list.length;
                if (soldier_count < 1 || soldier_count > 5) {
                    return next(null, {
                        code: Code.SOLDIER_INVALID_SELECT_COUNT
                    });
                }

                boss_model.setSelectSoldierList(select_soldier_list);
                boss_soldier_power = soldierService.calcSoldierPower(soldier_model, boss_model.select_soldier_list);
                boss_model.fight_power = boss_soldier_power;

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            var boss_model = player_manager.boss;
            return next(null, {
                code: Code.OK,
                result: {
                    boss_soldier_power: boss_soldier_power
                }
            });
        }
    );
};
